package com.black.utils.array;

import com.black.utils.random.RandomUtil;

/**
 * @author ylx
 */
public class ArrayUtil {
    /**
     * Shuffles the elements of the given array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(int[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            int temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given double array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的双精度浮点数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(double[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            double temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given float array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的浮点数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(float[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            float temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given long array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的长整型数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(long[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            long temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given short array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的短整型数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(short[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            short temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given byte array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的字节数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(byte[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            byte temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given boolean array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的布尔型数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(boolean[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            boolean temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given char array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的字符数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     */
    public static void shuffle(char[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            char temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    /**
     * Shuffles the elements of the given generic array in place using the Fisher-Yates algorithm.
     * <p>
     * 使用Fisher-Yates算法原地打乱给定的泛型数组的元素。
     *
     * @param array the array to be shuffled
     *              要打乱的数组
     * @param <T>   the type of elements in the array
     *              数组中元素的类型
     */
    public static <T> void shuffle(T[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int randomIndex = RandomUtil.nextInt(i);
            T temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }
}
